package com.aicre.wuliuapp.app.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.aicre.wuliuapp.app.MainActivity;
import com.aicre.wuliuapp.app.R;
import com.aicre.wuliuapp.app.util.Globles;
import com.aicre.wuliuapp.app.util.String2Request;
import com.android.volley.Response;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


public class SettingFragment extends BaseFragment {

    private SharedPreferences sp;
    private String id;
    private AlertDialog.Builder dialog;
    public SettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_setting, container, false);
        sp = this.getActivity().getSharedPreferences("56huowu", Activity.MODE_WORLD_READABLE);
        id = sp.getString("id", "-1").trim();

        RelativeLayout safety= (RelativeLayout) view.findViewById(R.id.account_safety);
        safety.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!id.equals("-1")){
                    dialog =new AlertDialog.Builder(getActivity());
                    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.dialog_passwd,null);
                    dialog.setView(layout);

                    final EditText new_passwd = (EditText)layout.findViewById(R.id.new_passwd);
                    final EditText old_passwd = (EditText)layout.findViewById(R.id.old_passwd);
                    final EditText con_passwd = (EditText)layout.findViewById(R.id.con_passwd);

                    dialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try
                            {
                                Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog, false);
                            }catch(Exception e) {
                                e.printStackTrace();
                            }


                            final String old_pwd=old_passwd.getText().toString();
                            final String new_pwd=new_passwd.getText().toString();
                            final String con_pwd=con_passwd.getText().toString();
                            if(new_pwd.equals(con_pwd)){
                                executeRequest(new String2Request(Globles.CHANGE_PWD, "utf-8", responseListener(),
                                        errorListener()) {
                                    protected Map<String, String> getParams() {
                                        Map<String, String> m = new HashMap<String, String>();
                                        m.put("sessionId",id);
                                        m.put("old_pwd",old_pwd);
                                        m.put("new_pwd",new_pwd);
                                        return m;
                                    }

                                });

                                try {
                                    Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                    field.setAccessible(true);
                                    field.set(dialog, true);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }



                            }else{

                                Toast.makeText(getActivity(),"请确认新输入的密码",Toast.LENGTH_SHORT).show();
                            }

                        }
                    });

                    dialog.show();
                }else{
                    Toast.makeText(getActivity(),"请先登录",Toast.LENGTH_SHORT).show();
                }

            }
        });




        return view;
    }



    private Response.Listener<String> responseListener(){
        return new Response.Listener<String>(){

            @Override
            public void onResponse(String s) {

                if(s.trim().equals("1")){
                    Toast.makeText(getActivity(),"修改成功",Toast.LENGTH_SHORT).show();
                }else if(s.trim().equals("2")){
                    Toast.makeText(getActivity(),"原密码错误",Toast.LENGTH_SHORT).show();
                }else if(s.trim().equals("0")){
                    Toast.makeText(getActivity(),"修改密码失败",Toast.LENGTH_SHORT).show();
                }

            }
        };
    }


}



